package com.xiaomage.handlerdemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import kotlinx.android.synthetic.main.activity_handler_thread_down.*

class HandlerThreadDownActivity : AppCompatActivity(), Handler.Callback {
    override fun handleMessage(msg: Message): Boolean {
        when(msg.what){
            0x110->{
                //开始下载
                tv_start.text=tv_start.text.toString()+"\n"+msg.obj
            }
            0x120->{
                tv_finish.text=tv_finish.text.toString()+"\n"+msg.obj
            }
        }
        return true
    }
    private var uiHandler:Handler?=null
    private var downHandlerThread:DownHandlerThread?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_handler_thread_down)
        //初始化
        downHandlerThread= DownHandlerThread("下载线程啦")
        uiHandler= Handler(this)
        downHandlerThread?.setUIHandlere(uiHandler)
        downHandlerThread?.setDownUrl("http://www.baidu.com/1",
                "http://www.baidu.com/2",
                "http://www.baidu.com/3",
                "http://www.baidu.com/4")
        bt_down.setOnClickListener{
            downHandlerThread?.start()
            bt_down.text="正在下载"
            bt_down.isEnabled=false
        }
    }
}
